/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessTier;

import DataTier.ContratoDAO;
import EntitiesTier.ClContrato;
import java.io.IOException;
import java.util.List;
/**
 *
 * @author Douglas
 */
public class ContratoBUS 
{
    public List<ClContrato> listar() throws Exception
    {
        try
        {
            ContratoDAO dao = new ContratoDAO();
            return dao.todos();
        }
        catch(IOException  e)
        {
            System.err.println("Erro: " + e.getMessage());
            return null;
        }    
    }
    
    public int inserir(ClContrato obj) throws Exception
    {
        try
        {
            if(valida_contrato(obj))
            {
                ContratoDAO dao = new ContratoDAO();
                obj.setCONT_COD(dao.incluir(obj));
            }
        }
        catch(IOException  e)
        {
            System.err.println("Erro: " + e.getMessage());
        }
        finally
        {
           return obj.getCONT_COD();
        }
    }
    
    public void atualizar(ClContrato obj) throws Exception
    {
        try
        {
            if(valida_contrato(obj))
            {
                ContratoDAO dao = new ContratoDAO();
                dao.atualizar(obj);            
            }
        }
        catch(IOException  e)
        {
            System.err.println("Erro: " + e.getMessage());
        }
    }
    
    public ClContrato buscar(int cod_codo) throws Exception
    {
        try
        {
            ContratoDAO dao = new ContratoDAO();
            return dao.procurar(cod_codo);
        }
        catch(IOException  e)
        {
            System.err.println("Erro: " + e.getMessage());
            return null;
        }
    }
    
    public ClContrato buscar(String codo_razao) throws Exception
    {
        try
        {
            ContratoDAO dao = new ContratoDAO();
            return dao.procurar(codo_razao);
        }
        catch(IOException  e)
        {
            System.err.println("Erro: " + e.getMessage());
            return null;
        }    
    }
    
    public void excluir(int cod_cont) throws Exception
    {
        try
        {
            ContratoDAO dao = new ContratoDAO();
            dao.excluir(cod_cont);
        }
        catch(IOException  e)
        {
            throw new Exception("Erro: " + e.getMessage());
        }
    }
    
    public boolean valida_contrato(ClContrato obj) throws Exception
    {
        if(obj.getCONT_CODFOR()== 0 )
        {
            throw new Exception("Deve preencher o Fornecedor.");
        }

        if(obj.getCONT_NOME() == null || obj.getCONT_NOME() == "")
        {
            throw new Exception("Deve preencher o Nome do Contrato.");
        }

        if(obj.getCONT_VALORINI() == 0)
        {
            throw new Exception("Deve preencher o Valor Inicial");
        }
        return true;       
    }
}
